<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button v-if="hasPerm('PB_StorArea.Add')" type="primary" icon="plus" @click="hanldleAdd()">新建</a-button>
      <a-button
        v-if="hasPerm('PB_StorArea.Delete')"
        type="primary"
        icon="minus"
        @click="handleDelete(selectedRowKeys)"
        :disabled="!hasSelected()"
        :loading="loading"
        >删除</a-button
      >
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
      <a-radio-group
        style="margin-right: 8px"
        default-value="asc"
        @change="getDataList()"
        button-style="solid"
        v-model="sorter.order"
      >
        <a-radio-button value="asc"> <a-icon type="arrow-up" /> 正序 </a-radio-button>
        <a-radio-button value="desc"> <a-icon type="arrow-down" /> 倒序 </a-radio-button>
      </a-radio-group>
      <a-popconfirm
        title="请选择打印的类型"
        cancel-text="批量A4"
        ok-text="批量条码"
        @confirm="confirms"
        @cancel="cancels"
      >
        <a-button
          type="primary"
          v-if="hasPerm('PB_StorArea.Print')"
          @click="rowsadd(selectedRowKeys)"
          :disabled="!hasSelecteds()"
          :loading="loading"
          >批量打印</a-button
        >
      </a-popconfirm>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-select v-model="AreaType" allowClear >
                <a-select-option
                  v-for="(item, index) in AreaTypeList"
                  :value="item.Id"
                  :key="index"
                >{{ item.DicName }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="Name" placeholder="库区编号或名称" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
              >查询</a-button
            >
            <a-button
              style="margin-left: 8px"
              @click="reset()"
              >重置</a-button
            >
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{ y: 560 }"
      :columns="columns"
      :rowKey="row => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      size="small"
    >
      <template slot="dataIndex" slot-scope="text, record, index">
        <span>{{ (pagination.current - 1) * pagination.pageSize + index + 1 }}</span>
      </template>
      <!-- <span slot="IsCache" slot-scope="text, record">
        <template>
          <a-tag v-if="record.IsCache===false" color="red">否</a-tag>
          <a-tag v-else color="green">是</a-tag>
        </template>
      </span> -->
      <template slot="Type" slot-scope="text">
        <enum-name code="StorAreaType" :value="text"></enum-name>
      </template>

      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="hasPerm('PB_Storage.Rack')" @click="openRackList(record.Id)">设置货架</a>
          <a-divider v-if="hasPerm('PB_Storage.Rack')" type="vertical" />
          <a v-if="hasPerm('PB_StorArea.Edit')" @click="handleEdit(record.Id)">编辑</a>
          <a-divider v-if="hasPerm('PB_StorArea.Edit')" type="vertical" />

          <a-popconfirm
            title="请选择打印的类型"
            cancel-text="打印A4"
            ok-text="打印条码"
            @confirm="confirm"
            @cancel="cancel"
          >
            <a v-if="hasPerm('PB_StorArea.Print')" @click="print(record.Id, record.Code)">打印</a></a-popconfirm
          >

          <a-divider v-if="hasPerm('PB_StorArea.Print')" type="vertical" />
          <a v-if="hasPerm('PB_StorArea.Delete')" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>
    <a-drawer
      width="30%"
      title="打印库区"
      :visible="boxflag"
      :confirm-loading="boxLoading"
      @ok="handleOk"
      @close="boxflag = false"
    >
      <div style="margin-bottom: 7px">
        <p>
          库区编号:
          <a-input v-model="Print.Code" disabled />
        </p>
        <a-input v-model="Print.StorAreaId" disabled v-show="false" />

        <p style="margin-bottom: 7px">
          打印机:
          <a-select style="width: 100%" v-model="Print.Pritntid">
            <a-select-option v-for="item in printlist" :key="item.Id" :value="item.Id">
              {{ item.Name }}
            </a-select-option>
          </a-select>
        </p>
      </div>
      <div style="height: 10px"></div>
      <div
        :style="{
          position: 'absolute',
          right: 0,
          bottom: 0,
          width: '100%',
          borderTop: '1px solid #e9e9e9',
          padding: '10px 16px',
          background: '#fff',
          textAlign: 'right',
          zIndex: 1
        }"
      >
        <a-button
          :style="{ marginRight: '8px' }"
          @click="
            () => {
              boxflag = false
            }
          "
          >取消</a-button
        >
        <a-button type="primary" @click="handleOk">打码</a-button>
      </div>
    </a-drawer>
    <edit-form ref="editForm" :parentObj="this"></edit-form>
    <material-list ref="materialList" :parentObj="this"></material-list>
    <rack-List ref="rackList"></rack-List>
    <DispatchPrint ref="dispatchPrint" :parentObj="this"></DispatchPrint>
    <BatchPrinting ref="batchPrinting" :parentObj="this"></BatchPrinting>
  </a-card>
</template>

<script>
import EditForm from './EditForm'
import RackList from '../PB_Rack/List'
import DispatchPrint from './DispatchPrint'
import BatchPrinting from './BatchPrinting'
const columns = [
  {
    title: '序号',
    dataIndex: 'aId',
    scopedSlots: { customRender: 'dataIndex' },
    width: '50px'
  },
  { title: '仓库', dataIndex: 'StorIdName', scopedSlots: { customRender: 'StorIdName' } },
  { title: '库区编号', dataIndex: 'Code' },
  { title: '库区名称', dataIndex: 'Name' },
  { title: '货区类型', dataIndex: 'TypeName' },
  // { title: '缓存区', dataIndex: 'IsCache', width: '10%', scopedSlots: { customRender: 'IsCache' } },
  { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' }, align: 'center', width: '22%' }
]

export default {
  components: {
    EditForm,
    RackList,
    DispatchPrint,
    BatchPrinting
  },
  data() {
    return {
      AreaTypeList: [],
      AreaType: '',
      Name: '',
      data: [],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], //设置pageSize的可选值，页面啊可以通过下拉框进行选择
        showSizeChanger: true,
        onShowSizeChange: (current, pageSize) => (this.pageSize = pageSize)
      },
      filters: {},
      sorter: { field: 'Id', order: 'asc' },
      loading: false,
      columns,
      queryParam: [],
      selectedRowKeys: [],
      ParentIdTreeData: [],
      Print: {},
      Pritntid: '',
      printlist: [],
      boxLoading: false,
      boxflag: false,
      printflag: false,
      disabled: true,
      confirmLoading: false,
      pid: '',
      codes: '',
      selectedRows: [],
      selectid: ''
    }
  },
  created() {
    this.getDataList()
    this.getprintlist()
    this.AreaTypeList = []
    this.$http.post('/Base_BasicData/Base_DictionaryMain/GetTheDataByDicKey', {
      Condition: 'DicKey',
      Keyword: 'StorAreaType'
    }).then(resJson => {
      this.loading = false
      this.AreaTypeList = resJson.Data.DictionaryDetailsList
    })
  },
  mounted() {
    this.getDataList()
    this.Organ()
  },
  methods: {
    rowsadd(id) {
      if (id.length == 0) {
        this.$message.warning('请选择要打印的库区')
        return
      }
      this.selectid = id
      this.Print.StorAreaId = id
      // this.boxflag = true
      // this.boxLoading = false
    },
    hasSelecteds() {
      return this.selectedRowKeys.length > 0
    },
    confirms() {
      if (this.selectedRowKeys.length == 0) {
        this.$message.warning('请选择要打印的库区')
        return
      }
      this.boxflag = true
      this.boxLoading = false
    },
    cancels() {
      if (this.selectedRowKeys.length == 0) {
        this.$message.warning('请选择要打印的库区')
        return
      }
      this.$refs.batchPrinting.openDrawer(this.selectid, '批量打印库区')
    },
    confirm() {
      this.boxflag = true
      this.boxLoading = false
    },
    cancel() {
      this.$refs.batchPrinting.openDrawer(this.pid, '打印货区')
    },

    async Organ() {
      await this.$http.post('/PB/PB_PackList/GetDetpList', {}).then(resJson => {
        if (resJson.Success) {
          this.ParentIdTreeData = resJson.Data
        }
      })
    },
    print(id, Code) {
      this.Print.StorAreaId = [id]
      this.Print.Code = Code
      this.pid = [id]
      this.codes = Code
      //  this.boxflag = true
      // this.boxLoading = false
    },
    handleOk() {
      this.$http.post('/PB/PB_StorArea/SelectAreaID', this.Print).then(res => {
        if (res.Success) {
          this.$message.success('打印成功')
          this.boxLoading = false
          this.boxflag = false

          this.getDataList()
        } else {
          this.boxLoading = false
          this.$message.error(res.Msg)
        }
      })
    },
    getprintlist() {
      this.$http.post('/PB/PB_JobBooking/GetPrinterListData?PrintType=' + 'PrintReservoir').then(res => {
        this.printlist = res.Data
      })
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.getDataList()
    },
    // SelectOrgan(SelectOrgan) {
    //   this.queryParam.StorId = SelectOrgan
    //   console.log(this.queryParam.StorId)
    // },
    reset(){
      this.AreaType=''
      this.Name=''
      this.getDataList()
    },
    getDataList() {
      this.selectedRowKeys = []
      this.queryParam = [{ Condition: 'Type', Keyword: this.AreaType }, { Condition: 'Name', Keyword: this.Name }]
      this.loading = true
      this.$http
        .post('/PB/PB_StorArea/GetStorAreaDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.Print.Code = selectedRows.map(item => item.Code).join(',')
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd() {
      this.$refs.editForm.openForm(null, '新增库区')
    },
    handleEdit(id) {
      console.log(id)
      this.$refs.editForm.openForm(id, '编辑库区')
    },
    openRackList(value) {
      this.$refs.rackList.openDrawer(value, this.sorter.areaId, '设置货架')
    },
    handleDelete(ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk() {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/PB/PB_StorArea/DeleteData', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    openMaterialList(areaId) {
      this.$refs.materialList.openDrawer(areaId)
    }
  }
}
</script>
<style>
@media screen and (max-width: 1366px) {
  .ant-select-tree {
    max-height: 500px;
  }
}
.ant-select-tree {
  max-height: 500px;
}
</style>
